package com.virtualtrainer_01.view;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.NumberPicker;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;

import com.virtualtrainer_01.R;
import com.virtualtrainer_01.dao.ClientDB;
import com.virtualtrainer_01.dao.SQLiteWrite;
import com.virtualtrainer_01.model.Client;

public class ClientInfo extends Activity implements OnClickListener {

	private NumberPicker numberPicker;

	private Button sqlUpdate;
	private EditText sqlName, sqlBirthday, sqlWeight, sqlHeight;
	private RadioButton sqlGenre;
	public Context context = ClientInfo.this;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.clientinfo);

		sqlUpdate = (Button) findViewById(R.id.b_confirm);
		sqlUpdate.setOnClickListener(this);
		sqlName = (EditText) findViewById(R.id.et_name);
		sqlBirthday = (EditText) findViewById(R.id.et_date);
		sqlWeight = (EditText) findViewById(R.id.et_weight);
		sqlHeight = (EditText) findViewById(R.id.et_height);
		sqlGenre = (RadioButton) findViewById(R.id.rb_male);
		sqlUpdate.setOnClickListener(this);
		numberPicker = (NumberPicker) findViewById(R.id.np_disp);
		numberPicker.setMaxValue(7);
		numberPicker.setMinValue(1);
		numberPicker.setWrapSelectorWheel(true);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.b_confirm:
			boolean didItWork = true;
			try {
				String name = sqlName.getText().toString();
				String birth = sqlBirthday.getText().toString();
				double height = Double.parseDouble(sqlHeight.getText().toString());
				double weight = Double.parseDouble(sqlWeight.getText().toString());
				boolean genre = false;
				if (sqlGenre.isChecked())
					genre = true;
				int dispon = numberPicker.getValue();
				SQLiteWrite sqlw = new SQLiteWrite(ClientInfo.this);
				sqlw.CreateClient();
				ClientDB entry = new ClientDB(ClientInfo.this);
				entry.open();
				entry.createEntry(new Client(name, birth, height, weight, genre, dispon));
				entry.close();
			} catch (Exception e) {
				didItWork = false;
				String error = "You didnt introduce all the details";
				Dialog d = new Dialog(this);
				d.setTitle("Error");
				TextView tv = new TextView(this);
				tv.setText(error);
				d.setContentView(tv);
				d.show();
			} finally {
				if (didItWork) {
					Toast.makeText(this, "Profile created successfully", Toast.LENGTH_LONG).show();
					Intent i = new Intent(this, Menu.class);
					startActivity(i);
				}
			}
		}
	}

	@Override
	protected void onPause() {
		Log.d("PAUSED", "Pressed");
		finish();
		super.onPause();
	}

}
